package com.intelligent.system.common.controller;

import com.intelligent.system.common.response.ResultDO;
import com.intelligent.system.common.utils.ThreadPoolUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Slf4j
@Api(tags = "线程池示例")
@RestController
@RequestMapping("/api/thread-pool")
@RequiredArgsConstructor
public class ThreadPoolController {
    private final ThreadPoolUtils threadPoolUtils;

    @ApiOperation("执行简单任务")
    @GetMapping("/execute")
    public ResultDO<Void> executeTask() {
        threadPoolUtils.execute(() -> {
            log.info("在线程 {} 中执行简单任务", Thread.currentThread().getName());
            // 模拟任务执行
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
        return ResultDO.success(null);
    }

    @ApiOperation("提交带返回值的任务")
    @GetMapping("/submit")
    public ResultDO<String> submitTask() {
        Future<String> future = threadPoolUtils.submit(() -> {
            log.info("在线程 {} 中执行带返回值的任务", Thread.currentThread().getName());
            // 模拟任务执行
            TimeUnit.SECONDS.sleep(1);
            return "Task completed successfully";
        });

        try {
            String result = future.get(5, TimeUnit.SECONDS);
            return ResultDO.success(result);
        } catch (Exception e) {
            log.error("任务执行失败", e);
            return ResultDO.error(500, "Task execution failed: " + e.getMessage());
        }
    }

    @ApiOperation("批量执行任务")
    @GetMapping("/batch")
    public ResultDO<List<String>> batchExecute() {
        List<Callable<String>> tasks = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            final int taskId = i;
            tasks.add(() -> {
                log.info("在线程 {} 中执行批量任务 {}", taskId, Thread.currentThread().getName());
                TimeUnit.SECONDS.sleep(1);
                return "Task " + taskId + " completed";
            });
        }

        try {
            List<Future<String>> futures = threadPoolUtils.invokeAll(tasks);
            List<String> results = new ArrayList<>();
            for (Future<String> future : futures) {
                results.add(future.get());
            }
            return ResultDO.success(results);
        } catch (Exception e) {
            log.error("批量执行失败", e);
            return ResultDO.error(500, "Batch execution failed: " + e.getMessage());
        }
    }

    @ApiOperation("获取线程池状态")
    @GetMapping("/status")
    public ResultDO<ThreadPoolUtils.ThreadPoolStatus> getStatus() {
        return ResultDO.success(threadPoolUtils.getStatus());
    }
} 